import winston from 'winston';
import 'winston-daily-rotate-file';
import { config } from '../config/index.js';

const { combine, timestamp, printf, colorize } = winston.format;

// 自定义日志格式
const logFormat = printf(({ level, message, timestamp }) => {
  return `${timestamp} [${level}]: ${message}`;
});

// 创建日志转储传输
const fileTransport = new winston.transports.DailyRotateFile({
  filename: config.logger.filename,
  datePattern: config.logger.datePattern,
  maxFiles: config.logger.maxFiles,
  format: combine(
    timestamp(),
    logFormat
  )
});

// 创建控制台传输
const consoleTransport = new winston.transports.Console({
  format: combine(
    colorize(),
    timestamp(),
    logFormat
  )
});

// 创建日志记录器
export const logger = winston.createLogger({
  level: config.logger.level,
  transports: [
    fileTransport,
    consoleTransport
  ]
}); 